/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.merb.server.serviceimpl;

import co.com.merb.client.service.LoginService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author alexkbra
 */
public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {

    @Override
    public Boolean ingresar(String nombre, String pass) {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("Usuario", nombre);
        return true;
    }

    @Override
    public String verificarSesion() throws RuntimeException {
        HttpServletRequest request = this.getThreadLocalRequest();
        HttpSession session = request.getSession(false);
        if(session!=null){
            String usuario = (String) session.getAttribute("Usuario");
            if (session == null || usuario == null) {
                return null;
            }
            return usuario;
        }
        return null;
    }

    @Override
    public Boolean cerrarSesion() throws RuntimeException {
        HttpServletRequest request = this.getThreadLocalRequest();
        // dont create a new one -> false
        HttpSession session = request.getSession(false);
        if (session == null) {
            return false;
        }
        // do some logout stuff ...
        session.invalidate();
        return true;
    }
}
